import os

def download_upload_action_panel_button ():

    global xrn_main_window_global
    slayout_local = xrn_main_window_global.jlayout_str

    selection = get_from_layout ( slayout_local, "download_upload_action_row_id" )["fields"][2]["selection"]

    obj = get_from_layout ( slayout_local, "download_from_website_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "dowload_directory_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "extraction_directory_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "download_website_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "upload_to_website_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "upload_directory_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "elaboration_directory_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "upload_website_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "extract_downloaded_content_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "elaborate_content_to_upload_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "download_archive_id" )
    obj["permission"] = "read_only" 
    obj["fields"][2]["direction"] = "input" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "extraction_archive_id" )
    obj["permission"] = "read_only" 
    obj["fields"][2]["direction"] = "input" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "upload_archive_id" )
    obj["permission"] = "read_only" 
    obj["fields"][2]["direction"] = "input" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "elaborate_archive_id" )
    obj["permission"] = "read_only" 
    obj["fields"][2]["direction"] = "input" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "file_to_elaborate_id" )
    obj["permission"] = "read_only" 
    obj["fields"][2]["direction"] = "input" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "generation_button_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "generate_string_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "generation_image_audio_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "resources_elaboration_directory_id" )
    obj["permission"] = "read_only" 
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "prev_elaboration_archive_id" )
    obj["permission"] = "read_only" 
    obj["fields"][2]["direction"] = "input" 
    slayout_local = set_to_layout( slayout_local, obj )

    if selection == "download from website" :

        obj = get_from_layout ( slayout_local, "download_from_website_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "dowload_directory_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "download_website_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "download_archive_id" )
        obj["permission"] = "read_write" 
        obj["fields"][2]["direction"] = "output" 
        slayout_local = set_to_layout( slayout_local, obj )

    elif selection == "extract content" :

        obj = get_from_layout ( slayout_local, "download_archive_id" )
        obj["permission"] = "read_write" 
        obj["fields"][2]["direction"] = "input" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "extraction_directory_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "extract_downloaded_content_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "extraction_archive_id" )
        obj["permission"] = "read_write" 
        obj["fields"][2]["direction"] = "output" 
        slayout_local = set_to_layout( slayout_local, obj )

    elif selection == "upload to website" :

        obj = get_from_layout ( slayout_local, "upload_to_website_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "upload_directory_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "upload_website_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "upload_archive_id" )
        obj["permission"] = "read_write" 
        obj["fields"][2]["direction"] = "output" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "upload_directory_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "elaborate_archive_id" )
        obj["permission"] = "read_write" 
        obj["fields"][2]["direction"] = "input" 
        slayout_local = set_to_layout( slayout_local, obj )

    elif selection == "elaborate content" :

        obj = get_from_layout ( slayout_local, "elaboration_directory_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "elaborate_content_to_upload_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "elaborate_archive_id" )
        obj["permission"] = "read_write" 
        obj["fields"][2]["direction"] = "output" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "file_to_elaborate_id" )
        obj["permission"] = "read_write" 
        obj["fields"][2]["direction"] = "input" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "resources_elaboration_directory_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "prev_elaboration_archive_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

    elif selection == "generate content" :

        obj = get_from_layout ( slayout_local, "generation_button_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "generate_string_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "generation_image_audio_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

        obj = get_from_layout ( slayout_local, "resources_elaboration_directory_id" )
        obj["permission"] = "read_write" 
        slayout_local = set_to_layout( slayout_local, obj )

    render_layout(  slayout_local )


def download_from_website_button () :
    print ("TODO")

def upload_to_website_button () :
    print ("TODO")

def extract_downloaded_content_button() :
    print ("TODO")

def generate_image_audio_button():
    print("TODO")

def elaborate_content_to_upload_button() :

    global xrn_main_window_global
    slayout_local = xrn_main_window_global.jlayout_str
    error = 0
    elaboration_archive_obj = {}

    panel_name = get_from_layout( slayout_local, "elaborate_content_to_upload_id" )["fields"][0]["display"]
    popup_obj, row_index = start_xtransfer_popup( panel_name , "input checks" )

    resources_elaboration_directory = get_from_layout( slayout_local, "resources_elaboration_directory_id" )["fields"][2]["display"]
    error, popup_obj = check_at_least_one_file( resources_elaboration_directory , row_index, popup_obj )

    if error == 0 :
        elaboration_directory = get_from_layout( slayout_local, "elaboration_directory_id" )["fields"][2]["display"]
        error, popup_obj = check_directory( elaboration_directory , row_index, popup_obj )

    if error == 0 :
        elaboration_archive = get_from_layout( slayout_local, "elaborate_archive_id" )["fields"][2]["display"]
        error, popup_obj = check_if_file_is_writable( elaboration_archive, row_index, popup_obj )

    # check if elaboration archive is active
    pre_elaboration_archive = {}
    if error == 0 :
        prev_elaboration_archive_path = get_from_layout( slayout_local, "prev_elaboration_archive_id" )["fields"][2]["display"]
        error_read, popup_obj = check_if_file_is_readable( prev_elaboration_archive_path, row_index, popup_obj )

        if error_read == 0 :

            with open( prev_elaboration_archive_path, 'r') as f:
                prev_elaboration_archive_content = f.read()

            if prev_elaboration_archive_content != "" :
                pre_elaboration_archive = json.loads(prev_elaboration_archive_content)
                text = "archive valid"

                if check_elaboration_archive( pre_elaboration_archive ) == 1 :
                    pre_elaboration_archive = {}
                    text = "archive not valid"

                rnd = str(random.randint(0, 10000))
                popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": os.path.basename(os.path.expanduser(prev_elaboration_archive_path)) + " archive content " + rnd, "data": text })

    # get the resources file list
    if error == 0 :
         popup_obj, row_index = add_row_xtransfer_popup( popup_obj, "input file detection" ) 
         error, files, checksums, rnds, png_flacs = get_files_and_checksums( resources_elaboration_directory )

         if error == 0 :
             if len(popup_obj[0]["rows"][row_index]["fields"]) <= 2 :
                 popup_obj[0]["rows"][row_index]["fields"].append({"type": "archive", "selection": rnds[0] + " path", "items":[]})

             for file, checksum, rnd, png_flac in zip(files, checksums, rnds, png_flacs ):
                 popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": rnd + " path", "data": file })
                 popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": rnd + " checksum", "data": checksum })
                 popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": rnd + " type", "data": png_flac })
                 elaboration_archive_obj["resources"] = []
                 msbs_path = elaboration_directory + "/" + rnd + ".msbs"
                 lsbs_path = elaboration_directory + "/" + rnd + ".lsbs"
                 meta_path = elaboration_directory + "/" + rnd + ".meta"
                 capacity_path = elaboration_directory + "/" + rnd + ".capacity"
                 elaboration_archive_obj["resources"].append({ "checksum": checksum, 
                                                           "source_path": file, 
                                                           "type": png_flacs, 
                                                           "msbs_path" : msbs_path,
                                                           "lsbs_path" : lsbs_path,
                                                           "metadata_path" : meta_path,
                                                           "capacity" : capacity_path
                                                           })

                 if pre_elaboration_archive == {} :
                     cmd = "xrnconv-cli --std2xrn -source " + file + " -msbs " + msbs_path + " -lsbs " + lsbs_path + " -metadata " + meta_path
                     os.system(cmd)

                     popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": rnd + " msbs", "data": msbs_path })
                     popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": rnd + " lsbs", "data": lsbs_path })
                     popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": rnd + " meta", "data": meta_path })

                     cmd = "xrnlib-cli --logging-conf -lib-info-log " + capacity_path + " --get-capacity -envelop-file " + lsbs_path 
                     os.system(cmd)

                     popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": rnd + " capacity", "data": capacity_path })

                 else :
                     for obj in pre_elaboration_archive["resources"] :
                         popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": rnd + " msbs", "data": obj["msbs_path"] })
                         popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": rnd + " lsbs", "data": obj["lsbs_path"] })
                         popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": rnd + " meta", "data": obj["metadata_path"] })
                         popup_obj[0]["rows"][row_index]["fields"][2]["items"].append({"display": rnd + " meta", "data": obj["capacity"] })


    # get the output file list

    popup_obj = rename_final_popup_panel_name (panel_name, popup_obj, error)
    popup_str = json.dumps(popup_obj)

    return popup_str


